/**
 * classification 数据
 */
import { classificationAjax, itemAjax, categoryItemAjax, categoryListAjax, detailAjax } from '@/api/classification.js'
export default {
  namespaced: true,
  state: {
    botherIndex: 0, // categoryItem页面中，对应选中的索引
    categoryList: [],
    currentCategory: [],
    currentSubCategory: [],
    brotherCategory: [],
    parentCategory: [],
    filterCategoryList: [],
    list: [],
    paging: {
      total: 0,
      pages: 0,
      page: 0,
      limit: 0
    },
    detailData: {
      specificationList: [],
      groupon: [],
      issue: [],
      userHasCollect: 0,
      shareImage: '',
      commet: [],
      share: false,
      attribute: [],
      brand: [],
      productList: [],
      info: []
    }

  },
  mutations: {
    commitClassificationData (state, result) {
      state.categoryList = result.categoryList
      // 给categoryList中每个对象上去映射一个text属性
      state.categoryList.map(item => {
        item.text = item.name
      })
    },
    commitItemsData (state, result) {
      state.currentCategory = result.currentCategory
      state.currentSubCategory = result.currentSubCategory
    },
    commitCatetoryItemData (state, result) {
      state.brotherCategory = result.brotherCategory
      state.currentCategory = result.currentCategory
      state.parentCategory = result.parentCategory
      state.botherIndex = result.index
    },
    commitCategoryListData (state, result) {
      state.filterCategoryList = result.filterCategoryList
      state.paging.total = result.total
      state.paging.pages = result.pages
      state.paging.page = result.page
      state.paging.limit = result.limit
      state.list = result.list
    },
    commitDetailData (state, result) {
      state.detailData.specificationList = result.specificationList
      state.detailData.groupon = result.groupon
      state.detailData.issue = result.issue
      state.detailData.userHasCollect = result.userHasCollect
      state.detailData.shareImage = result.shareImage
      state.detailData.commet = result.commet
      state.detailData.share = result.share
      state.detailData.attribute = result.attribute
      state.detailData.brand = result.brand
      state.detailData.productList = result.productList
      state.detailData.info = result.info
      console.log(state)
    }
  },
  actions: {
    // 获取左侧导航栏数据
    getClassificationData ({ commit }) {
      classificationAjax().then(res =>
        commit('commitClassificationData', res.data)
      ).catch(err => {
        console.log(err)
      })
    },
    // 获取右侧主体内容数据
    getItemsData (context, index) {
      itemAjax(context.state.categoryList[index].id).then(res => {
        context.commit('commitItemsData', res.data)
      })
    },
    // 获取catetoryItem中数据
    getCatetoryItemData ({ commit }, { id, index }) {
      categoryItemAjax(id).then(res => {
        commit('commitCatetoryItemData', { ...res.data, index: index })
      })
    },
    // 获取商品列表数据
    getCategoryListData ({ commit }, { id, page, limit }) {
      categoryListAjax({ id, page, limit }).then(res => {
        commit('commitCategoryListData', res.data)
      })
    },
    // 商品详情页面数据
    getDetailData ({ commit }, id) {
      console.log(id)
      detailAjax(id).then(res => {
        console.log(res.data)
        commit('commitDetailData', res.data)
      }).catch(err => {
        console.log(err)
      })
    }
  },
  getters: {}
}
